package org.lc.cmd.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lc.cmd.ReceivedSysReq;
import org.lc.cmd.SysReqHandler;
import org.lc.cmd.SysReqHandlerChain;
import org.lc.cmd.exp.DevException;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
public class DefaultSysReqHandlerChain implements SysReqHandlerChain {
    private final List<SysReqHandler> handlers;

    private int index = 0;

    @Override
    public void next(ReceivedSysReq req) {
        if (index >= handlers.size()) {
            throw new DevException("No next handler. You'll need a correct default handler to handle this sysReq, id: " + req.getId() + ", type: " + req.getType());
        }
        handlers.get(index++).handle(req, this);
    }
}
